package com.freecloud.javabasics.network.socket.demo1;

import com.freecloud.common.LoggerUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @Author: maomao
 * @Date: 2020-03-15 11:25
 */
public class ClentSocketDemo1 {
    public static void main(String[] args) {
        try {
            //找到目标的IP和端口
            Socket socket = new Socket("127.0.0.1",8080);
            //获得网络输出流
            PrintWriter out = getOutputStream(socket) ;
            //获得网络输入流
            BufferedReader in = getInputStream(socket);

            //获取控制台输入流
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            //获取控制台输入流内容
            String sysMsg = sin.readLine();



            while (!sysMsg.equals("bye")){
                LoggerUtil.println("获取控制台输入：" + sysMsg);
                out.println(sysMsg);
                LoggerUtil.println("接收到服务端信息：" + in.readLine());
                //重新获取控制台输入
                sysMsg = sin.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static PrintWriter getOutputStream(Socket socket) throws IOException {
        return new PrintWriter(socket.getOutputStream());
    }

    public static BufferedReader getInputStream(Socket socket) throws IOException {
        //拿到输入流
        return new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

}
